﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.Web.coffinegurunaru.AppLogic.Page;
using Com.Library.Cookie;
using Com.Library.DB.Board;
using Com.Framework;
using Com.Framework.File;

namespace Com.Web.coffinegurunaru.whatsnew
{
	public partial class notice_write : WebPage
	{
		public ArticleEntity Article { get; set;}
		public int ArticleSN { get; set;}
		public int PageNo { get; set;}
		public string SearchWord { get ;set; }
		public string SearchType { get ;set; }
		protected override void OnInit(EventArgs e)
		{
            base.OnInit(e);
            if (!this.WebCookies.IsLogin)
            {
               Response.Redirect("/manager/login.aspx?redirect=" + Server.UrlEncode(Request.Url.ToString()));
            }

			ArticleSN = Request["articlesn"].Parse(0);
			PageNo = Request["pageNo"].Parse(1);

			if (ArticleSN > 0)
			{
				Article = ArticleModel.GetInfo(ArticleSN, false, null);

				if (Article == null)
				{
					ClientScript.RegisterStartupScript(this.GetType(), "ArticleNone", "alert('글이 없습니다.'); location.href='/whatsnew/notice.aspx?articlesn="+ ArticleSN+"&PageNo="+PageNo+"'", true);
					return;
				}

				txtSubject.Text = Article.strTitle;
				txtContent.Text = Article.strContent;
			}
			this.LeftMenuType = 2;
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			SearchWord = Request["searchWord"];
			SearchType = Request["searchType"].Parse("0");

			btnSubmit.Click += new ImageClickEventHandler(btnSubmit_Click);
			btnCancel.Click += new ImageClickEventHandler(btnCancel_Click);
		}

		void btnCancel_Click(object sender, ImageClickEventArgs e)
		{
			if (ArticleSN > 0 && Article != null)
			{
				Response.Redirect("/whatsnew/notice_view.aspx?articlesn="+ArticleSN+"&PageNo="+PageNo+"&Searchtype=" + SearchType + "&searchWord=" + SearchWord);
			}
			else
			{
				Response.Redirect("/whatsnew/notice.aspx?PageNo="+PageNo+"&Searchtype=" + SearchType + "&searchWord=" + SearchWord);
			}
		}

		void btnSubmit_Click(object sender, ImageClickEventArgs e)
		{
			string title = txtSubject.Text;
			string content = txtContent.Text;
			HttpPostedFile postedFile = file.PostedFile;
			string fileUrl = string.Empty;

			if (ArticleSN > 0 && Article != null)
			{
				if (postedFile.ContentLength > 0)
				{
					fileUrl = FileManager.BoardUpload(postedFile, (byte)ArticleModel.BoardSNEnum.Notice, ArticleSN);
				}
				ArticleModel.Modify(Article.n4ArticleSN, title, content, string.Empty, fileUrl);

				Response.Redirect("/whatsnew/notice_view.aspx?articlesn="+ArticleSN+"&PageNo="+PageNo+"&Searchtype=" + SearchType + "&searchWord=" + SearchWord);
			}
			else
			{
				ArticleSN = ArticleModel.Create((byte)ArticleModel.BoardSNEnum.Notice, title, content, string.Empty, WebCookies.ManagerSN, string.Empty, string.Empty);
				if (postedFile.ContentLength > 0)
				{
					fileUrl = FileManager.BoardUpload(postedFile, (byte)ArticleModel.BoardSNEnum.Notice, ArticleSN);
				}
				ArticleModel.Modify(ArticleSN, null, null, null, fileUrl);

				Response.Redirect("/whatsnew/notice.aspx");
			}


		}
		
		public string GetFileName(string fileUrl)
		{
			string[] urlChunks = fileUrl.Split('/');

			return urlChunks[urlChunks.Length-1];
		}

		private ManagerCookie m_WebCookie = null;
        public new ManagerCookie WebCookies
        {
            get
            {
                if (m_WebCookie == null)
                {
                    m_WebCookie = new ManagerCookie();
                }

                return m_WebCookie;
            }
        }
	}
}
